<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\VariantAttribute;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;

class AdminSpecificationController extends Controller
{
    /**
     * 显示规格属性列表
     */
    public function index(): Response
    {
        $attributes = VariantAttribute::with(['product'])
            ->orderBy('created_at', 'desc')
            ->paginate(20)
            ->through(function ($attribute) {
                return [
                    'id' => $attribute->id,
                    'name' => $attribute->name,
                    'slug' => $attribute->slug,
                    'type' => $attribute->type,
                    'is_required' => $attribute->is_required,
                    'is_variant' => $attribute->is_variant,
                    'sort_order' => $attribute->sort_order,
                    'options' => $attribute->options,
                    'product' => $attribute->product ? [
                        'id' => $attribute->product->id,
                        'name' => $attribute->product->name,
                    ] : null,
                    'created_at' => $attribute->created_at->format('Y-m-d H:i:s'),
                ];
            });

        return Inertia::render('Admin/Specifications/Index', [
            'attributes' => $attributes,
        ]);
    }

    /**
     * 显示创建规格属性表单
     */
    public function create(): Response
    {
        return Inertia::render('Admin/Specifications/Create');
    }

    /**
     * 存储新的规格属性
     */
    public function store(Request $request)
    {
        $request->validate([
            'name' => 'required|string|max:255',
            'slug' => 'required|string|max:255|unique:variant_attributes,slug',
            'type' => 'required|in:select,color,size,text,number',
            'options' => 'nullable|array',
            'is_required' => 'boolean',
            'is_variant' => 'boolean',
            'sort_order' => 'integer|min:0',
            'meta_data' => 'nullable|array',
        ]);

        VariantAttribute::create([
            'name' => $request->name,
            'slug' => $request->slug,
            'type' => $request->type,
            'options' => $request->options,
            'is_required' => $request->boolean('is_required'),
            'is_variant' => $request->boolean('is_variant'),
            'sort_order' => $request->sort_order ?? 0,
            'meta_data' => $request->meta_data,
        ]);

        return redirect()->route('specifications.index')
            ->with('success', '规格属性创建成功！');
    }

    /**
     * 显示编辑规格属性表单
     */
    public function edit(VariantAttribute $specification): Response
    {
        return Inertia::render('Admin/Specifications/Edit', [
            'specification' => [
                'id' => $specification->id,
                'name' => $specification->name,
                'slug' => $specification->slug,
                'type' => $specification->type,
                'options' => $specification->options,
                'is_required' => $specification->is_required,
                'is_variant' => $specification->is_variant,
                'sort_order' => $specification->sort_order,
                'meta_data' => $specification->meta_data,
            ],
        ]);
    }

    /**
     * 更新规格属性
     */
    public function update(Request $request, VariantAttribute $specification)
    {
        $request->validate([
            'name' => 'required|string|max:255',
            'slug' => 'required|string|max:255|unique:variant_attributes,slug,' . $specification->id,
            'type' => 'required|in:select,color,size,text,number',
            'options' => 'nullable|array',
            'is_required' => 'boolean',
            'is_variant' => 'boolean',
            'sort_order' => 'integer|min:0',
            'meta_data' => 'nullable|array',
        ]);

        $specification->update([
            'name' => $request->name,
            'slug' => $request->slug,
            'type' => $request->type,
            'options' => $request->options,
            'is_required' => $request->boolean('is_required'),
            'is_variant' => $request->boolean('is_variant'),
            'sort_order' => $request->sort_order ?? 0,
            'meta_data' => $request->meta_data,
        ]);

        return redirect()->route('specifications.index')
            ->with('success', '规格属性更新成功！');
    }

    /**
     * 删除规格属性
     */
    public function destroy(VariantAttribute $specification)
    {
        // 检查是否有关联的变体属性值
        if ($specification->attributeValues()->exists()) {
            return redirect()->route('specifications.index')
                ->with('error', '该规格属性正在被使用，无法删除！');
        }

        $specification->delete();

        return redirect()->route('specifications.index')
            ->with('success', '规格属性删除成功！');
    }

    /**
     * 切换规格属性状态
     */
    public function toggleStatus(VariantAttribute $specification)
    {
        $specification->update([
            'is_required' => !$specification->is_required,
        ]);

        return redirect()->route('specifications.index')
            ->with('success', '规格属性状态更新成功！');
    }
}
